<?php
namespace app\admin\controller;

use app\common\model\BisAccount;
use app\common\model\BisLocation;
use app\common\model\City;
use app\common\model\Bis;
use think\Controller;
use think\Request;
use app\common\model\Category;


class Store extends Controller
{


    public function index()
    {
        $locations = model('BisLocation')->getBisLocationByStatus(1);
        $this->assign('locations', $locations);
        return $this->fetch();
    }

    public function apply()
    {
        $locations = model('BisLocation')->getBisLocationByStatus();
        $this->assign('locations', $locations);
        return $this->fetch();
    }

    public function deleted()
    {
        $locations = model('BisLocation')->getBisLocationByStatus(-1);
        $this->assign('locations', $locations);
        return $this->fetch();
    }

    public function detail(Request $request)
    {
        $id = $request->param('id');
        if (empty($id))
        {
            return $this->error('ID错误');
        }
        // 获取一级城市数据
        $citys = model('City')->getNormalCitysByparentId();
        // 获取一级栏目数据
        $categorys = model('Category')->getNormalCategorysByparentId();
        // 获取商户数据
        $locationData = model('BisLocation')->get(['id' => $id]);
        $bisData = model('Bis')->get(['id' => $locationData['bis_id']]);
        $accountData = model('BisAccount')->get(['id' => $bisData['id']]);
        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys,
            'bisData' => $bisData,
            'locationData' => $locationData,
            'accountData' => $accountData
        ]);
        return $this->fetch();
    }

    public function status(Request $request)
    {
        $data = $request->param();
        // TODO:数据校验
        /*$validate = validate('Bis');
        if (!$validate->scene('status')->check($data))
        {
            $this->error($validate->getError());
        }*/
        $res = model('BisLocation')->save(['status' => $data['status']],['id' => $data['id']]);
        if ($res)
        {
            // TODO:发送邮件
            // status 2 通过 status -1 删除
            $this->success('状态更新成功');
        }
        else
        {
            $this->error('状态更新失败');
        }
    }

    public function delete(Request $request)
    {
        $id = $request->param('id');
        $res = model('BisLocation')->save(['status' => -1], ['id' => $id]);
        if ($res)
        {
            $this->success('下架成功');
        }
        else
        {
            $this->error('下架失败');
        }
    }
}
